package com.brainhealth.customer.service.impl;

import com.brainhealth.common.core.domain.TreeSelect;
import com.brainhealth.common.enums.GoodsType;
import com.brainhealth.common.enums.YNEnum;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.RoomUserProperty;
import com.brainhealth.customer.domain.ScoreMallGoods;
import com.brainhealth.customer.domain.ScoreMallGoodsCategory;
import com.brainhealth.customer.domain.vo.GoodsCategoryTreeVO;
import com.brainhealth.customer.mapper.RoomUserPropertyMapper;
import com.brainhealth.customer.mapper.ScoreMallGoodsCategoryMapper;
import com.brainhealth.customer.mapper.ScoreMallGoodsMapper;
import com.brainhealth.customer.service.IScoreMallGoodsCategoryService;
import com.brainhealth.utils.CommonUtils;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 积分商城商品分类Service业务层处理
 * 
 * @author wolong
 * @date 2023-11-01
 */
@Service
public class ScoreMallGoodsCategoryServiceImpl implements IScoreMallGoodsCategoryService 
{
    @Autowired
    private ScoreMallGoodsCategoryMapper scoreMallGoodsCategoryMapper;
    @Autowired
    private ScoreMallGoodsMapper scoreMallGoodsMapper;
    @Autowired
    private RoomUserPropertyMapper roomUserPropertyMapper;

    /**
     * 查询积分商城商品分类
     * 
     * @param id 积分商城商品分类主键
     * @return 积分商城商品分类
     */
    @Override
    public ScoreMallGoodsCategory selectScoreMallGoodsCategoryById(Long id)
    {
        return scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryById(id);
    }

    /**
     * 查询积分商城商品分类列表
     * 
     * @param scoreMallGoodsCategory 积分商城商品分类
     * @return 积分商城商品分类
     */
    @Override
    public List<ScoreMallGoodsCategory> selectScoreMallGoodsCategoryList(ScoreMallGoodsCategory scoreMallGoodsCategory)
    {
        List<ScoreMallGoodsCategory> lstData = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(scoreMallGoodsCategory);
        return lstData;
    }

    @Override
    public List<TreeSelect> getScoreMallGoodsCategoryTree()
    {
        ScoreMallGoodsCategory query = new ScoreMallGoodsCategory();
        List<ScoreMallGoodsCategory> lstData = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(query);
        List<TreeSelect> lstTree = new ArrayList<>();
        for (ScoreMallGoodsCategory root : lstData)
        {
            if (root.getParentCategoryId().equals(0l))
            {
                TreeSelect rootNode = new TreeSelect();
                rootNode.setLabel(root.getCategoryName());
                rootNode.setId(root.getCategoryId());
                CommonUtils.getTreeSelectChildren(rootNode,lstData,"getCategoryId","getCategoryName","getParentCategoryId");
                lstTree.add(rootNode);
            }
        }
        return lstTree;
    }

    @Override
    public List<GoodsCategoryTreeVO> getScoreMallGoodsCategoryTreeList(HttpServletRequest request)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        ScoreMallGoodsCategory query = new ScoreMallGoodsCategory();
        List<ScoreMallGoodsCategory> lstAllData = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(query);
        ScoreMallGoods queryGoods = new ScoreMallGoods();
        queryGoods.setYn(YNEnum.NO.getCode());
        List<ScoreMallGoods> lstAllGoods = scoreMallGoodsMapper.selectScoreMallGoodsList(queryGoods);
        List<GoodsCategoryTreeVO> lstTree = new ArrayList<>();
        for (ScoreMallGoodsCategory root : lstAllData)
        {
            if (root.getParentCategoryId().equals(0l))
            {
                getChildGoodsCategory(root, lstTree, lstAllData);
            }
        }
        for (GoodsCategoryTreeVO tree : lstTree)
        {
            List<ScoreMallGoods> lstGoods = lstAllGoods.stream().filter(item -> item.getCategoryId().equals(tree.getCategoryId())).collect(Collectors.toList());
            if (lstGoods.size() > 0)
            {
                RoomUserProperty roomUserProperty = new RoomUserProperty();
                roomUserProperty.setCustomerId(customer.getCustomerId());
                roomUserProperty.setYn(YNEnum.NO.getCode());
                List<RoomUserProperty> lstCustomerProperty = roomUserPropertyMapper.selectRoomUserPropertyList(roomUserProperty);
                for (ScoreMallGoods goods: lstGoods)
                {
                    RoomUserProperty customerGoods = lstCustomerProperty.stream().filter(item -> item.getGoodsId().equals(goods.getGoodsId())).findFirst().orElse(null);
                    if (customerGoods != null && !customerGoods.getGoodsType().equals(GoodsType.AIPackage.getCode()))
                    {
                        goods.setHave(true);
                    } else
                    {
                        goods.setHave(false);
                    }
                    tree.getLstGoods().add(goods);
                }
            }
        }
        return lstTree;
    }

    public void getChildGoodsCategory(ScoreMallGoodsCategory node, List<GoodsCategoryTreeVO> lstData, List<ScoreMallGoodsCategory> lstAllData)
    {
        List<ScoreMallGoodsCategory> lstChild = lstAllData.stream().filter(item -> item.getParentCategoryId().equals(node.getCategoryId())).collect(Collectors.toList());
        for (ScoreMallGoodsCategory child : lstChild)
        {
            if (child.getChildren() == null || child.getChildren().size() == 0)
            {
                GoodsCategoryTreeVO treeData = new GoodsCategoryTreeVO();
                BeanUtils.copyProperties(child,treeData);
                lstData.add(treeData);
            }
            getChildGoodsCategory(child, lstData, lstAllData);
        }
    }

    @Override
    public List<ScoreMallGoodsCategory> getScoreMallGoodsCategoryParentList(Long categoryId)
    {
        ScoreMallGoodsCategory query = new ScoreMallGoodsCategory();
        List<ScoreMallGoodsCategory> lstData = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(query);
        ScoreMallGoodsCategory category = lstData.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst().orElse(null);
        List<ScoreMallGoodsCategory> lstParent = new ArrayList<>();
        if (category != null)
        {
            getParentNode(category, lstData, lstParent);
        }
        return lstParent;
    }

    public void getParentNode(ScoreMallGoodsCategory node,List<ScoreMallGoodsCategory> lstData, List<ScoreMallGoodsCategory> lstParent)
    {
        ScoreMallGoodsCategory parentNode = lstData.stream().filter(item -> item.getCategoryId().equals(node.getParentCategoryId())).findFirst().orElse(null);
        if (parentNode != null)
        {
            lstParent.add(0, parentNode);
            getParentNode(parentNode, lstData, lstParent);
        }
    }

    public void getChildNode(ScoreMallGoodsCategory node, List<ScoreMallGoodsCategory> lstNode)
    {
        List<ScoreMallGoodsCategory> lstChild = lstNode.stream().filter(item -> item.getParentCategoryId().equals(node.getCategoryId())).collect(Collectors.toList());
        if (node.getChildren() == null)
        {
            node.setChildren(new ArrayList<>());
            node.setChildren(lstChild);
        }
        for (ScoreMallGoodsCategory child : lstChild)
        {
            getChildNode(child, lstNode);
        }
    }

    /**
     * 新增积分商城商品分类
     * 
     * @param scoreMallGoodsCategory 积分商城商品分类
     * @return 结果
     */
    @Override
    public int insertScoreMallGoodsCategory(ScoreMallGoodsCategory scoreMallGoodsCategory)
    {
        Long count = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryCount();
        scoreMallGoodsCategory.setCreateTime(DateUtils.getNowDate());
        scoreMallGoodsCategory.setCategoryId(count);
        scoreMallGoodsCategory.setYn(0);
        return scoreMallGoodsCategoryMapper.insertScoreMallGoodsCategory(scoreMallGoodsCategory);
    }

    /**
     * 修改积分商城商品分类
     * 
     * @param scoreMallGoodsCategory 积分商城商品分类
     * @return 结果
     */
    @Override
    public int updateScoreMallGoodsCategory(ScoreMallGoodsCategory scoreMallGoodsCategory)
    {
        scoreMallGoodsCategory.setUpdateTime(DateUtils.getNowDate());
        //根据商品分类的层级修改商品的层级
        ScoreMallGoods query = new ScoreMallGoods();
        query.setCategoryId(scoreMallGoodsCategory.getCategoryId());
        List<ScoreMallGoods> lstGoods = scoreMallGoodsMapper.selectScoreMallGoodsList(query);
        int result = scoreMallGoodsCategoryMapper.updateScoreMallGoodsCategory(scoreMallGoodsCategory);
        if (result > 0)
        {
            for (ScoreMallGoods goods: lstGoods)
            {
                goods.setLayer(scoreMallGoodsCategory.getLayer());
                scoreMallGoodsMapper.updateScoreMallGoods(goods);
            }
            return result;
        }
        return result;
    }

    /**
     * 批量删除积分商城商品分类
     * 
     * @param ids 需要删除的积分商城商品分类主键
     * @return 结果
     */
    @Override
    public int deleteScoreMallGoodsCategoryByIds(Long[] ids)
    {
        return scoreMallGoodsCategoryMapper.deleteScoreMallGoodsCategoryByIds(ids);
    }

    /**
     * 删除积分商城商品分类信息
     * 
     * @param id 积分商城商品分类主键
     * @return 结果
     */
    @Override
    public int deleteScoreMallGoodsCategoryById(Long id)
    {
        return scoreMallGoodsCategoryMapper.deleteScoreMallGoodsCategoryById(id);
    }
}
